package com.justinwang4.androidservicedemo.activities;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.justinwang4.androidservicedemo.R;
import com.justinwang4.androidservicedemo.services.MyIntentBindService;
import com.justinwang4.androidservicedemo.services.MyIntentService;

public class MyIntentServiceActivity extends AppCompatActivity {

    private Button btnStartService;
    private Button btnStopService;

    private Button btnBindService;
    private Button btnUnbindService;

    private Intent intentForStart;
    private Intent intentForBind;

    // Bind
    private Boolean mBound = false;
    private MyIntentBindService.MyIntentBinder myIntentBinder;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i("Test", "onServiceConnected");
            myIntentBinder = (MyIntentBindService.MyIntentBinder) iBinder;
            String content = myIntentBinder.getStringInfo();
            Toast.makeText(MyIntentServiceActivity.this, content, Toast.LENGTH_SHORT).show();
            mBound = true;
        }

        /**
         * onServiceDisconnected只会在Service丢失时才会调用， 通常会在Service所在进程被迫终止时才会调用
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i("Test", "onServiceDisconnected");
            myIntentBinder = null;
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_service);

        btnStartService = findViewById(R.id.btn_start_service);
        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MyIntentServiceActivity.this, "start service...", Toast.LENGTH_SHORT).show();
                intentForStart = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
                startService(intentForStart);
            }
        });

        btnStopService = findViewById(R.id.btn_stop_service);
        btnStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MyIntentServiceActivity.this, "stop service...", Toast.LENGTH_SHORT).show();
                intentForStart = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
                stopService(intentForStart);
            }
        });

        btnBindService = findViewById(R.id.btn_bind_service);
        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MyIntentServiceActivity.this, "bind service...", Toast.LENGTH_SHORT).show();
                intentForBind = new Intent(MyIntentServiceActivity.this, MyIntentBindService.class);
                bindService(intentForBind, serviceConnection, Context.BIND_AUTO_CREATE);
            }
        });

        btnUnbindService = findViewById(R.id.btn_unbind_service);
        btnUnbindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String toastMsg = "unbind service...";
                if (mBound) {
                    unbindService(serviceConnection);
                    mBound = false;
                } else {
                    toastMsg = "no bind yet, please bind first!!!";
                }
                Toast.makeText(MyIntentServiceActivity.this, toastMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
